package com.fjrcloud.service;

import com.fjrcloud.dao.TemplateCurrentDao;
import com.fjrcloud.dao.TemplateWarehouseDao;
import com.fjrcloud.entity.TemplateCurrent;
import com.fjrcloud.entity.TemplateWarehouse;
import com.fjrcloud.exception.BusinessException;
import com.fjrcloud.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * Created by shengjian.lin on 16-12-30.
 */
@Service
public class TemplateService {

    @Autowired
    private TemplateCurrentDao currentDao;

    @Autowired
    private TemplateWarehouseDao warehouseDao;

    /**
     * 保存模板
     * @param template 模板
     * @return
     */
    public TemplateCurrent save(TemplateWarehouse template) throws BusinessException {

        TemplateCurrent current;
        if (template.getId() == null){
            current = get(template.getName());
            if(current != null){
                throw new BusinessException(template.getName()+" 模板已经存在");
            }
        }else{
            current = get(template.getId());
        }
        if (current != null){
            template.setId(null);
            template.setName(current.getName());
//            template.setCode(current.getCode());
            template.setVersion(current.getWarehouse().getVersion()+1);
        }else{
            template.setVersion(1);
        }
        template.setCreateTime(System.currentTimeMillis());
        template = warehouseDao.save(template);

        if (current == null){
            current = new TemplateCurrent();
            current.setName(template.getName());
//            current.setCode(template.getCode());
        }
        current.setUpdateTime(template.getCreateTime());
        current.setTemplate(template.getTemplate());
        current.setVersion(template.getVersion());
        current.setWarehouse(template);

        return currentDao.save(current);
    }

    public TemplateWarehouse getWarehouse(Integer id){
        return warehouseDao.findOne(id);
    }

    /**
     * 获取模板
     * @param name 模板名称
     * @return
     */
    public TemplateCurrent get(String name){
        return currentDao.findByNameAndValid(name,true);
    }

    /**
     * 获取模板
     * @param id 模板ID
     * @return
     */
    public TemplateCurrent get(Integer id){
        return currentDao.findOne(id);
    }

    /**
     * 获取模板列表
     * @return
     */
    public Page<TemplateCurrent> list(Integer pageNum, Integer pageSize){

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return currentDao.findByValid(true,pageRequest);
    }

    /**
     * 获取模板的所有版本
     * @param id
     * @param pageNum
     * @param pageSize
     * @return
     * @throws BusinessException
     */
    public Page<TemplateWarehouse> versions(Integer id,Integer pageNum, Integer pageSize) throws BusinessException {

        TemplateCurrent current = currentDao.findOne(id);
        if (current == null)
            throw new BusinessException();

        return versions(current.getName(),pageNum,pageSize);
    }
    public Page<TemplateWarehouse> versions(String name,Integer pageNum, Integer pageSize){

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return warehouseDao.findByNameAndValidOrderByVersionDesc(name,true,pageRequest);
    }
}
